package com.rambo.sdm.controller;

import com.rambo.sdm.common.BaseController;
import com.rambo.sdm.dao.pojo.UserPO;
import com.rambo.sdm.service.inter.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;


@Controller
@RequestMapping("/user")
public class UserAction extends BaseController {

    private UserService userService;

    @Autowired
    public UserAction(UserService userService) {
        Assert.notNull(userService, "userService is required!");
        this.userService = userService;
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String addUser(UserPO user) {
        user.setYxbz("Y");
        log.info("添加用户成功，添加的用户成功:" + userService.addUser(user));
        return "default";
    }

    @RequestMapping("/get/users")
    public String getUserList(Model model) {
        List<UserPO> userList = userService.getUserList();
        log.info("获取所有用户成功，用户数量为:" + userList.size());

        for (UserPO userPO : userList) {
            if (userPO.getPhone() != null) {
                log.info("该用户电话不为空：" + userPO.toString());
            } else {
                userPO.setPhone("123456789");
                userService.updateUser(userPO);
                log.info("更新用户电话后:" + userService.findUserByUuid(userPO.getUuid()));

                userService.deleteUserById(userPO.getUuid());
                log.info("删除该用户成功后:" + userService.findUserByUuid(userPO.getUuid()));
            }
        }
        model.addAttribute("userList", userList);
        return "default";
    }
}